/**
 * chinagoods.com
 * Copyright (c) 2020-2024 All Rights Reserved.
 */
package com.ruoyi.system.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.RequestUtil;
import com.ruoyi.system.biz.CashHistoryBizService;
import com.ruoyi.system.biz.TGBizService;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.service.CashHistoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangpeng
 * Created by on 2024-01-30 13:04
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CashHistoryBizServiceImpl extends BaseBizServiceImpl implements CashHistoryBizService {


    private final CashHistoryService cashHistoryService;

    private final TGBizService tgBizService;


    @Override
    public void checkError(Date start, Date end) {
        tgBizService.sendWarnMsg("招财猫--开始检查，当前时间："+DateUtil.now()+"\n"+"检查数据日期为："+ DateUtil.formatDate(start));
        List<Long> userIds = cashHistoryService.selectUserIds(start, end);
        if (CollectionUtil.isEmpty(userIds)) {
            return;
        }
        for (int i = 0; i < userIds.size(); i++) {
            Long userId = userIds.get(i);
            CashHistory cashHistory = new CashHistory();
            cashHistory.setUserId(userId);

            PageDomain pageDomain = new PageDomain();
            pageDomain.setPageSize(2000);
            pageDomain.setBeginTime(DateUtil.formatDateTime(start));
            pageDomain.setEndTime(DateUtil.formatDateTime(end));
            pageDomain.setIsAsc("asc");
            pageDomain.setOrderByColumn("id");
            List<CashHistory> list = cashHistoryService.findList(cashHistory, pageDomain);
            for (int j = 0; j < list.size(); j++) {
                if (j == list.size() - 1) {
                    continue;
                }
                cashHistory = list.get(j);
                CashHistory next = list.get(j + 1);

                //现在值，
                BigDecimal earning = next.getEarning();
                BigDecimal changeMoney = next.getChangeMoney();
                if (!StrUtil.equals(next.getChangeType(), "add")) {//如果是减少  变成
                    changeMoney = BigDecimal.ZERO.subtract(changeMoney);
                }
                BigDecimal add = cashHistory.getEarning().add(changeMoney);

                if (add.compareTo(earning) != 0) {
                    BigDecimal subtract = earning.subtract(add);
                    String s = errorMsg(userId + "", cashHistory.getOrderNo(), add.toString(), earning.toString(), subtract.toString());

                    //sendMsg(userId + "", cashHistory.getOrderNo(), add.toString(), earning.toString(), subtract.toString());

                    String errorMsg= errorMsg(userId + "", cashHistory.getOrderNo(), add.toString(), earning.toString(), subtract.toString());
                    tgBizService.sendWarnMsg(errorMsg);


                }
            }


        }
        sendWarnMsg("招财猫--结束检查，当前时间："+DateUtil.now());

    }

    @Override
    public void checkError() {
        DateTime end = DateUtil.beginOfDay(new Date());
        DateTime start = DateUtil.offsetDay(end, -1);

        checkError(start, end);

    }

    private String errorMsg(String userId, String orderNo, String orgin, String now, String sub) {
        String msg = "用户id：userId\n订单号：orderNo\n原值：orgin\n现值：now\n差额：sub";
        msg = msg.replace("userId", userId + "")
                .replace("orderNo", orderNo)
                .replace("orgin", orgin)
                .replace("now", now)
                .replace("sub", sub);
        return  msg;
    }

//    private static void sendMsg(String userId, String orderNo, String orgin, String now, String sub) {
//        String msg = "用户id：userId\n订单号：orderNo\n原值：orgin\n现值：now\n差额：sub";
//        msg = msg.replace("userId", userId + "")
//                .replace("orderNo", orderNo)
//                .replace("orgin", orgin)
//                .replace("now", now)
//                .replace("sub", sub);
//        Map<String, Object> map = new HashMap<>();
//        map.put("text", msg);
//        //招财猫群
//        map.put("chatId", "-4280960558");
//        try {
//            RequestUtil.postJson("http://43.156.231.185:8088/api/orderPay/warn1", map);
//
//        } catch (Exception e) {
//            log.info("错误信息:{}", e.getMessage());
//        }
//    }

    private static void sendWarnMsg(String msg) {

        Map<String, Object> map = new HashMap<>();
        map.put("text", msg);
        //招财猫群
        map.put("chatId", "-4280960558");
        try {
            RequestUtil.postJson("http://43.156.231.185:8088/api/orderPay/warn1", map);

        } catch (Exception e) {
            log.info("错误信息:{}", e.getMessage());
        }
    }

//    public static void main(String[] args) {
//        sendMsg("1", "23123", "43434", "2323", "12");
//    }
}